/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.own.service;

/**
 *
 * @author Maxim A. Kanev <ask.me.makes@gmail.com> [mkanev]
 */
public class Statistics {

    private final static double[][] T = {
        {0, 0, 0, 0, 0}, //1
        {0, 0, 0, 0, 0}, //2
        {0, 0, 0, 0, 0}, //3
        {2.132, 2.776, 3.747, 4.604, 8.610}, //4
        {2.015, 2.571, 3.365, 4.032, 6.859}, //5
        {1.943, 2.447, 3.143, 3.707, 5.959}, //6
        {1.895, 2.365, 2.998, 3.499, 5.405}, //7
        {1.860, 2.306, 2.896, 3.355, 5.041}, //8
        {1.833, 2.262, 2.821, 3.250, 4.781}, //9
        {1.812, 2.228, 2.764, 3.169, 4.587}, //10
        {1.796, 2.201, 2.718, 3.106, 4.487}, //11
        {1.782, 2.179, 2.681, 3.055, 4.318}, //12
        {1.771, 2.160, 2.650, 3.012, 4.221}, //13
        {1.761, 2.145, 2.624, 2.977, 4.140}, //14
        {1.753, 2.131, 2.602, 2.947, 4.073}, //15
        {1.746, 2.120, 2.583, 2.921, 4.015}, //16
        {0, 0, 0, 0, 0}, //17
        {1.734, 2.103, 2.552, 2.878, 3.922}, //18
        {0, 0, 0, 0, 0}, //19
        {1.725, 2.086, 2.528, 2.845, 3.850}, //20
        {0, 0, 0, 0, 0}, //21
        {0, 0, 0, 0, 0}, //22
        {0, 0, 0, 0, 0}, //23
        {0, 0, 0, 0, 0}, //24
        {1.708, 2.060, 2.485, 2.787, 3.725}, //25
        {0, 0, 0, 0, 0}, //26
        {0, 0, 0, 0, 0}, //27
        {0, 0, 0, 0, 0}, //28
        {0, 0, 0, 0, 0}, //29
        {1.697, 2.042, 2.457, 2.750, 3.646} //30
    };

    public static double getT(double p, int n) {
        if (((n >= 1) && (n <= 3)) || (n == 17) || (n == 19)
                || ((n >= 21) && (n <= 24)) || ((n >= 26) && (n <= 29))) {
            return 0.0d;
        }
        int i = 0;
        if (Math.abs(p - 0.9) < 0.000001) {
            i = 1;
        } else if (Math.abs(p - 0.95) < 0.000001) {
            i = 2;
        } else if (Math.abs(p - 0.98) < 0.000001) {
            i = 3;
        } else if (Math.abs(p - 0.99) < 0.000001) {
            i = 4;
        } else if (Math.abs(p - 0.999) < 0.000001) {
            i = 5;
        }
        return T[n][i];
    }

    public static int[] arrayFromString(String input) {
        if (input == null || input.length() == 0) {
            return new int[0];
        }
        String[] inputArray = input.split(" ");
        int[] result = new int[inputArray.length];
        for (int i = 0; i < inputArray.length; i++) {
            String value = inputArray[i];
            if (value != null && !"".equals(value)) {
                result[i] = Integer.valueOf(value);
            }
        }
        return result;
    }

    public static String stringFromArray(int[] inputArray) {
        if (inputArray == null || inputArray.length == 0) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        for (int i : inputArray) {
            result.append(String.valueOf(i)).append(" ");
        }
        return result.toString();
    }

    public static String formatOutput(String output) {
        if (output == null || output.length() == 0) {
            return output;
        }
        StringBuilder result = new StringBuilder();
        String[] temp = output.split("\\.");
        result.append(temp[0]);
        if (temp.length > 1) {
            result.append(".");
            result.append((temp[1].length() > 2 ? temp[1].substring(0, 2) : temp[1]));
        }
        return result.toString();
    }
}
